package com.ems.remote.components;

import com.ems.bo.FrameBo;
import com.ems.remote.IStrategy;
import com.ems.service.IDeviceService;
import com.ems.util.ConstUtil;
import com.ems.util.HexUtil;
import com.ems.vo.DeviceVo;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class ConnectStrategy implements IStrategy {

    @Resource
    private IDeviceService iDeviceService;

    /**
     * 设备上行连接确认帧数据
     * @param frameBo：解析后的帧数据内容
     * @param channelHandlerContext
     * @return
     */
    @Override
    public void action(FrameBo frameBo, ChannelHandlerContext channelHandlerContext) {
        log.info("连接确认帧，[{}, {}]", frameBo.getCommandCode(), frameBo.getData());
        //解析错误码是否为01正常 0901
        String str = HexUtil.parseData ( frameBo.getData (), 2, 1 );
        if (ConstUtil.SUCCESS_CODE.equals(str)) {
            // 查询设备是否存在
            DeviceVo deviceVo = new DeviceVo ();
            deviceVo.setDeviceAddress ( frameBo.getDeviceAddress () );
            deviceVo = iDeviceService.querySingle ( deviceVo );
            if ( deviceVo != null ) {
                // 设备确认已登录成功，更新设备登录状态
                iDeviceService.updateSingle(deviceVo);
            }
        }
    }
}
